package controllers.manage.recharge;

import cn.emay.common.Convert;
import controllers.BaseController;
import controllers.auth.Secure;
import enums.GoodsType;
import helper.GlobalConfig;
import models.merchant.Merchant;
import models.weixin.WebUser;
import models.weixin.amount.RechargeCombo;
import org.apache.commons.lang.StringUtils;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;
import util.common.ConvertUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员卡充值
 * Created by hanbin on 17/4/28.
 */
@With(BaseController.class)
public class ReChargeCtrl extends Controller {

    /**
     * 跳转到会员卡充值页面
     */
    public static void index(Integer pageNumber, String nickName, String phone) {

        Merchant merchant = Secure.getLoginUser().merchant;

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();

        if(StringUtils.isNotBlank(nickName)){
            searchMap.put("nickName", "%"+nickName+"%");
        }

        if(StringUtils.isNotBlank(phone)){
            searchMap.put("phone", phone);
        }

        if(merchant != null){
            searchMap.put("merchantId", merchant.id);
        }

        flash.put("menuId", "16");

        JPAExtPaginator<WebUser> resultPage = WebUser.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, nickName, phone);
    }

    /**
     * 跳转到会员卡充值明细
     */
    public static void detail(long id , GoodsType goodsType){
        WebUser webUser = WebUser.findById(id);
        List<RechargeCombo> rechargeComboList = RechargeCombo.loadByGoodsType(goodsType);
        render(webUser, id , rechargeComboList);
    }

    //判断充值金额是否有赠送
    public static void checkIsGive(Double amount){
        Map<String , Object> resultMap = new HashMap<>();
        RechargeCombo combo = RechargeCombo.findByRechargeAmount(amount);
        Double giveAmount = 0d ;
        String message = "";
        if(combo != null){
            giveAmount = combo.giveAmount ;
            message = "充值"+amount+"元 , 赠送"+giveAmount+"元 ,共计"+(amount+giveAmount)+"元!<br/>您是否确认充值?" ;

        }
        resultMap.put("giveAmount",giveAmount);
        resultMap.put("message",message);
        renderJSON(ConvertUtil.toJson(resultMap));

    }

}
